<?php

namespace App\Models;

use App\Models\Traits\HasCustomStorage;
use App\Models\Traits\HasEasyStatus;
use Dcat\Admin\Models\Administrator;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class Application extends Model
{
    use SoftDeletes,
        Cachable,
        HasEasyStatus,
        HasCustomStorage;

    protected $casts = [
        'expired_at' => 'datetime',
    ];

    protected static function boot(): void
    {
        parent::boot();

        self::creating(function (Application $app) {
            $app->app_key = Str::random();
            $app->app_secret = Str::random(32);
        });
    }

    public function admin(): BelongsTo
    {
        return $this->belongsTo(Administrator::class, 'admin_id');
    }

    public function aliyun(): HasOne
    {
        return $this->hasOne(Aliyun::class)
            ->orderByDesc('is_default');
    }

    public function domainPool(): BelongsTo
    {
        return $this->belongsTo(DomainPool::class);
    }
}
